package main

import "fmt"

//#link: https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/
func main() {
	nums := []int{5, 7, 7, 8, 8, 10}
	target := 6
	nums = []int{1, 3}
	target = 1
	fmt.Println(searchRange(nums, target))
}

func searchRange(nums []int, target int) []int {
	result := []int{-1, -1}
	left, right := 0, len(nums)
	for left < right {
		mid := (right-left)>>1 + left
		//当中间数比目标数小的时候
		if nums[mid] < target {
			left = mid + 1
		} else if nums[mid] > target {
			right = mid
		} else {
			for left <= mid {
				if nums[left] == target {
					result[0] = left
					break
				}
				left++
			}
			for right >= mid {
				if nums[right-1] == target {
					result[1] = right - 1
					break
				}
				right--
			}
			break
		}
	}
	return result
}
